package com.shuoyue.ycgk.net.base.filedownload;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import okhttp3.ResponseBody;
import retrofit2.Response;

public class DownloadHelper {
    public interface DownloadApkListener {
        void onStart();

        void onProgress(int p);

        void onFinish(String path);

        void onError(String msg);
    }


    public static class FileDownloadRun implements Runnable {
        Response<ResponseBody> mResponseBody;
        DownloadApkListener mDownloadApkListener;
        String savePath;
        public FileDownloadRun(Response<ResponseBody> responseBody,
                               final DownloadApkListener downloadApkListener,String savePath) {
            mResponseBody = responseBody;
            mDownloadApkListener = downloadApkListener;
            this.savePath =savePath;
        }

        @Override
        public void run() {
            writeResponseBodyToDisk(mResponseBody.body(), mDownloadApkListener,savePath);
        }
    }


    /**
     * @param body
     * @param downloadListener
     * @return
     */
    private static boolean writeResponseBodyToDisk(ResponseBody body, final DownloadApkListener downloadListener,String savePath) {
        if (downloadListener != null)
            downloadListener.onStart();
        try {
            // 改成自己需要的存储位置
            File file = new File(savePath);
            if (file.exists()) {
                file.delete();
            }
            InputStream inputStream = null;
            OutputStream outputStream = null;

            try {
                byte[] fileReader = new byte[4096];

                long fileSize = body.contentLength();
                long fileSizeDownloaded = 0;

                inputStream = body.byteStream();
                outputStream = new FileOutputStream(file);

                while (true) {
                    int read = inputStream.read(fileReader);

                    if (read == -1) {
                        break;
                    }

                    outputStream.write(fileReader, 0, read);

                    fileSizeDownloaded += read;

                    //计算当前下载百分比，并经由回调传出
                    if (downloadListener != null)
                        downloadListener.onProgress((int) (100 * fileSizeDownloaded / fileSize));
                }

                if (downloadListener != null)
                    downloadListener.onFinish(file.getPath());
                outputStream.flush();

                return true;
            } catch (IOException e) {
                if (downloadListener != null)
                    downloadListener.onError("" + e.getMessage());
                return false;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }

                if (outputStream != null) {
                    outputStream.close();
                }
            }
        } catch (IOException e) {
            return false;
        }
    }
}
